package com.dds.web.controller.union;

import java.util.List;
import java.util.Map;

import com.dds.common.utils.ServletUtils;
import com.dds.framework.web.service.TokenService;
import com.dds.system.domain.search.union.UnionProductSearch;
import com.dds.system.service.union.IUnionProductJournalService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dds.common.annotation.Log;
import com.dds.common.core.controller.BaseController;
import com.dds.common.core.domain.AjaxResult;
import com.dds.common.enums.BusinessType;
import com.dds.system.domain.union.UnionProduct;
import com.dds.system.service.union.IUnionProductService;
import com.dds.common.core.page.TableDataInfo;

/**
 * 联盟商家积分商品Controller
 * 
 * @author gaolianjie
 * @date 2021-04-30
 */
@RestController
@RequestMapping("/union/product")
public class UnionProductController extends BaseController
{
    @Autowired
    private IUnionProductService unionProductService;

    @Autowired
    private IUnionProductJournalService unionProductJournalService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询联盟商家积分商品列表
     */
    @PreAuthorize("@ss.hasPermi('union:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(UnionProductSearch unionProduct)
    {
        startPage();
        List<Map<String, Object>> list = unionProductService.selectUnionProductList(unionProduct);
        return getDataTable(list);
    }

    /**
     * 获取联盟商家积分商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('union:product:query')")
    @GetMapping(value = "/{prodId}")
    public AjaxResult getInfo(@PathVariable("prodId") Integer prodId)
    {
        return AjaxResult.success(unionProductService.selectUnionProductById(prodId));
    }

    /**
     * 新增联盟商家积分商品
     */
    @PreAuthorize("@ss.hasPermi('union:product:add')")
    @Log(title = "联盟商家积分商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UnionProduct unionProduct)
    {
        return toAjax(unionProductService.insertUnionProduct(unionProduct));
    }

    /**
     * 修改联盟商家积分商品
     */
    @PreAuthorize("@ss.hasPermi('union:product:edit')")
    @Log(title = "联盟商家积分商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UnionProduct unionProduct)
    {
        return toAjax(unionProductService.updateUnionProduct(unionProduct));
    }

    /**
     * 修改积分商品状态
     */
    @PreAuthorize("@ss.hasPermi('union:product:changeStatus')")
    @PutMapping(value = "/changeStatus")
    @Log(title = "联盟商家积分商品", businessType = BusinessType.UPDATE)
    public AjaxResult changeProdStatus(@RequestBody Map<String, Object> params) {
        return toAjax(unionProductService.changeUnionProduct(
                params,
                tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId()
        ));
    }

    @GetMapping(value = "/showLog")
    public TableDataInfo showLog(
            @RequestParam(name = "prodId") Integer prodId
    ) {
        startPage();

        return getDataTable(unionProductJournalService.selectUnionProductJournalListByProdId(prodId));
    }
}
